package com.mxw.mytools.web;

import com.mxw.mytools.dto.ExcelReadAndProcessAndWriteRequest;
import com.mxw.mytools.dto.ExcelReadAndProcessAndWriteResponse;
import com.mxw.mytools.dto.ExcelReadResponse;
import com.mxw.mytools.service.ExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @Autowired
    private ExcelService excelService;

    @PostMapping("/test1")
    public String test1() {
        String result = "";
        ExcelReadResponse excelReadResponse = excelService.readExcel("C:\\Users\\m6553\\Desktop\\gsp_letter_box.csv");
        for (Map<Integer, String> data : excelReadResponse.getDatas()) {
            String requestContent = data.get(6);

            String prox = "<source_id>.*?</source_id>";

            Pattern compile = Pattern.compile(prox);
            Matcher matcher = compile.matcher(requestContent);

            while (matcher.find()) {
                String group = matcher.group(0);
                String replace = group.replace("<source_id>", "").replace("</source_id>", "");
                result = result + ",'" + replace +"'";
            }
        }

        System.out.println(result);
        return "ok";
    }

    public static void main(String[] args) {
        String source = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:kth=\"kthrp.kthis.interface.soap\">\n" +
                "    <soapenv:Header/>\n" +
                "    <soapenv:Body>\n" +
                "        <kth:HIPMessageServer>\n" +
                "            <kth:action>KTHRPXMSLogisticsSync@CY_PROD</kth:action>\n" +
                "            <kth:message>\n" +
                "            <![CDATA[\n" +
                "<Request>\n" +
                "    <id root=\"2.16.156.10011.0\" extension=\"14985287-cbc7-4a10-8892-5fa68a27be1b\"/>\n" +
                "    <creationTime value=\"20240826103657\"/>\n" +
                "    <messageTypeCode code=\"ZDY\"/>\n" +
                "    <sender typeCode=\"SND\">\n" +
                "        <device classCode=\"DEV\" determinerCode=\"INSTANCE\">\n" +
                "            <id root=\"2.16.156.10011.0\" extension=\"14985287-cbc7-4a10-8892-5fa68a27be1b\"/>\n" +
                "        </device>\n" +
                "    </sender>\n" +
                "    <receiver typeCode=\"RCV\">\n" +
                "        <device classCode=\"DEV\" determinerCode=\"INSTANCE\">\n" +
                "            <id root=\"2.16.156.10011.0\" extension=\"14985287-cbc7-4a10-8892-5fa68a27be1b\"/>\n" +
                "        </device>\n" +
                "    </receiver>\n" +
                "    <Body xsi:type=\"kthrpxmsLogisticsSync332\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "        <company>H07</company>\n" +
                "        <delivery_date>2024-08-26 10:36:56</delivery_date>\n" +
                "        <rows>\n" +
                "            <row>\n" +
                "                <business_type>0</business_type>\n" +
                "                <po_number>CYPO-240801975</po_number>\n" +
                "                <product_number>CC000137</product_number>\n" +
                "                <process_qty>10</process_qty>\n" +
                "                <unit>盒</unit>\n" +
                "                <lot_number>B240403-1</lot_number>\n" +
                "                <expiry_date>2025-09-30 00:00:00</expiry_date>\n" +
                "                <manufacture_date>2024-04-09 00:00:00</manufacture_date>\n" +
                "                <source_id>1261</source_id>\n" +
                "            </row>\n" +
                "            <row>\n" +
                "                <business_type>0</business_type>\n" +
                "                <po_number>CYPO-240801975</po_number>\n" +
                "                <product_number>CC015022</product_number>\n" +
                "                <process_qty>5</process_qty>\n" +
                "                <unit>盒</unit>\n" +
                "                <lot_number>20240505</lot_number>\n" +
                "                <expiry_date>2026-04-30 00:00:00</expiry_date>\n" +
                "                <manufacture_date>2024-05-15 00:00:00</manufacture_date>\n" +
                "                <source_id>1262</source_id>\n" +
                "            </row>\n" +
                "            <row>\n" +
                "                <business_type>0</business_type>\n" +
                "                <po_number>CYPO-240801975</po_number>\n" +
                "                <product_number>CC000053</product_number>\n" +
                "                <process_qty>4</process_qty>\n" +
                "                <unit>盒</unit>\n" +
                "                <lot_number>20221101</lot_number>\n" +
                "                <expiry_date>2025-10-31 00:00:00</expiry_date>\n" +
                "                <manufacture_date>2022-11-25 00:00:00</manufacture_date>\n" +
                "                <source_id>1264</source_id>\n" +
                "            </row>\n" +
                "        </rows>\n" +
                "    </Body>\n" +
                "</Request>\n" +
                "]]>\n" +
                "\n" +
                "</kth:message>\n" +
                "</kth:HIPMessageServer>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>";


        String prox = "<source_id>.*?</source_id>";

        Pattern compile = Pattern.compile(prox);
        Matcher matcher = compile.matcher(source);

        while (matcher.find()) {
            String group = matcher.group(0);
            String replace = group.replace("<source_id>", "").replace("</source_id>", "");
        }

    }


}
